package com.katze.boot.zookeeper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.katze.boot.zookeeper.dao.ZookeeperMapper;
import com.katze.boot.zookeeper.entity.ZookeeperEntity;
import com.katze.boot.zookeeper.service.ZookeeperService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ZookeeperServiceImpl extends ServiceImpl<ZookeeperMapper, ZookeeperEntity> implements ZookeeperService {

    @Override
    public List<ZookeeperEntity> findByOwner(Long owner) {
        LambdaQueryWrapper<ZookeeperEntity> wrapper = Wrappers.lambdaQuery(ZookeeperEntity.class)
                .eq(ZookeeperEntity::getOwner, owner)
                .orderByDesc(ZookeeperEntity::getCreateTime);
        return this.list(wrapper);
    }

    @Override
    public ZookeeperEntity exists(ZookeeperEntity zookeeper) {
        LambdaQueryWrapper<ZookeeperEntity> wrapper = Wrappers.lambdaQuery(ZookeeperEntity.class)
                .eq(ZookeeperEntity::getServer, zookeeper.getServer())
                .eq(ZookeeperEntity::getAuthorization, zookeeper.getAuthorization())
                .eq(ZookeeperEntity::getOwner, zookeeper.getOwner())
                .ne(StringUtils.isNotBlank(zookeeper.getId()), ZookeeperEntity::getId, zookeeper.getId());
        return this.getOne(wrapper);
    }
}
